package com.ruoyi.mp.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.web.MpClientCart;
import com.ruoyi.db.domain.web.MpClientSubCart;
import com.ruoyi.mp.annotion.Login;
import com.ruoyi.mp.annotion.LoginUser;
import com.ruoyi.mp.service.IMpClientCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户购物车Controller
 * 
 * @author ruoyi
 * @date 2022-08-02
 */
@Api(value = "用户购物车",tags ="用户购物车")
@RestController
@RequestMapping("/smp/cart")
public class MpClientCartController extends BaseController
{
    private static Logger log = LoggerFactory.getLogger(MpClientCartController.class);
    @Autowired
    private IMpClientCartService mpClientCartService;

    /**
     * 查询用户购物车列表
     */
    @Login
    @ApiOperation(value = "查询用户购物车")
    @GetMapping("/myCart")
    public AjaxResult myCart(@LoginUser MpClient client)
    {
        try {
            MpClientCart cart = mpClientCartService.selectMpClientCartByClientId(client.getClientId());
            return AjaxResult.success(cart);
        }catch (Exception e){
            log.error("用户购物车发生异常",e);
        }
        return AjaxResult.error("用户购物车异常");
    }


    /**
     * 新增用户购物车
     */
    @Login
    @ApiOperation(value = "新增用户购物车")
    @PostMapping
        public AjaxResult add(@RequestBody MpClientSubCart subCart, @LoginUser MpClient client)
    {
        try {
            mpClientCartService.insertMpClientCart(subCart,client.getClientId());
            return AjaxResult.success("添加成功");
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改用户购物车
     */
    @Login
    @ApiOperation(value = "更新购物车商品数量")
    @PutMapping
    public AjaxResult edit(@RequestBody MpClientSubCart subCart)
    {
        try {
            mpClientCartService.updateMpClientCart(subCart);
            return AjaxResult.success("数量更新成功");
        }catch (Exception e){
            log.error("修改用户购物车发生异常",e);
        }
        return AjaxResult.error("用户购物车异常");
    }
    /**
     * 删除【购物车内商品】
     */
    @Login
    @ApiOperation(value = "删除【购物车内商品】")
    @DeleteMapping("/{subCartIds}")
    public AjaxResult remove(@PathVariable Long[] subCartIds,@LoginUser MpClient client)
    {
        try {
            mpClientCartService.deleteMpClientCartByCartIds(subCartIds,client.getClientId());
            return AjaxResult.success("删除成功");
        }catch (Exception e){
            log.error("删除【购物车内商品】发生异常",e);
        }
        return AjaxResult.error("删除【购物车内商品】异常");
    }

}
